package com.exp.sys.service.department;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import tk.mybatis.mapper.entity.Example;

import com.exp.base.annotation.TransactionalRead;
import com.exp.sys.entity.Admin;
import com.exp.sys.entity.Department;
import com.exp.sys.entity.Role;
import com.exp.sys.mapper.AdminMapper;
import com.exp.sys.mapper.DepartmentMapper;
import com.exp.sys.mapper.RoleMapper;
import com.exp.sys.service.BaseService;
import com.exp.sys.vo.department.DepartmentTreeVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Component("departmentService")
public class DepartmentService  extends DepartmentBaseService{
	private void addTreeItem(DepartmentTreeVo departmentTreeVo,List<Department>  departmentList) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		Iterator<Department> departmentIte = departmentList.iterator();
		while(departmentIte.hasNext()){
			Department department = departmentIte.next();
			if(StringUtils.equals(departmentTreeVo.getId(),department.getParentId())){
				DepartmentTreeVo _departmentTreeVo = new DepartmentTreeVo();
				PropertyUtils.copyProperties(_departmentTreeVo, department);
				departmentIte.remove();
				departmentTreeVo.getChildren().add(_departmentTreeVo);
				addTreeItem(_departmentTreeVo,new ArrayList<Department>(departmentList));
			};
		}

	}
	
	public void updateWithChildParentIds(Department department,String sourcesDepartmentParentIds){
		update(department);
		updateFullId(sourcesDepartmentParentIds,department.getParentIds());
	}

	
	@TransactionalRead
	public List<DepartmentTreeVo> queryTree(){
		Example example = new Example(Department.class);
		example.createCriteria().andEqualTo("deleted", false);
		List<Department>  departmentList = new ArrayList<Department>(query(example));
		Iterator<Department> departmentIte = departmentList.iterator();
		
		List<DepartmentTreeVo> treeItemList = new ArrayList<DepartmentTreeVo>();
		while(departmentIte.hasNext()){
			Department department = departmentIte.next();
			if(StringUtils.isEmpty(department.getParentId())){
				DepartmentTreeVo departmentTreeVo = new DepartmentTreeVo();
				try {
					PropertyUtils.copyProperties(departmentTreeVo, department);
				} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
					throw new RuntimeException(e);
				}
				treeItemList.add(departmentTreeVo);
				departmentIte.remove();
			};
		}
		
		for(DepartmentTreeVo departmentTreeVo : treeItemList){
			try {
				addTreeItem(departmentTreeVo,departmentList);
			} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
				throw new RuntimeException(e);
			}
		}
		
		
		
		return treeItemList;
	}
	
	public void changeTreeNode(String childId,String parentId){
		Department chidlDepartment = get(childId);
		String sourcesDepartmentFullId = chidlDepartment.getFullId();
		Assert.notNull(chidlDepartment, "不存在子部门");
		Example isExistParentId = new Example(Department.class);
		if(StringUtils.isNotEmpty(parentId)){
			isExistParentId.createCriteria().andEqualTo("id", parentId);
			Assert.isTrue(exist(isExistParentId),"不存在父部门");
		}
		chidlDepartment.setParentId(parentId);
		update(chidlDepartment);
		updateFullId(sourcesDepartmentFullId,chidlDepartment.getFullId());
	}
}
